package com.huali.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


@WebServlet(value = "/login")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //进行账号密码校验
        if ("123".equals(username)) {
            if ("123".equals(password)) {
                //创建cookie cookie以键值对的形式存放
                Cookie cookie = new Cookie(username, password);
                //将cookie添加到客服端
                response.addCookie(cookie);

                //获取session
                HttpSession session = request.getSession();
                Map<String, String> map = new HashMap<>();
                map.put(username, password);
                //将数据存放在session中
                session.setAttribute("user", map);
//filter
                //进行页面跳转 转发
                request.getRequestDispatcher("/admin.html").forward(request, response);
                return;
            }
        }
        //重定向
        response.sendRedirect("/error.html");


        response.getWriter().write("服务器响应成功！！！");
    }
}
